package org.client.ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import org.client.tools.ClientTool;
import org.client.tools.DateProcess;
import org.pojo.MessageType;
import org.pojo.User;
import org.pojo.Message.Message;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.AttributeSet.FontAttribute;
import javax.swing.text.StyleConstants.FontConstants;

/**
 * @author 周亮
 * @time 2012-6-18 下午12:33:46
 *
 */
public class IMChatView extends JFrame implements KeyListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * @wbp.nonvisual location=57,7
	 */
	//private final JLabel label = DefaultComponentFactory.getInstance().createTitle("聊天室");
	private User my = new User("fefeffe","男","做事认真，能挣钱；做人太认真，只会受伤；做人应该像演戏，虚假但轻松。","5.gif",23);
	private JTextPane messageConPane ;
	private User friend;
	private JTextPane send_message;
	private JScrollPane scrollPane;
	private Font myfont = new Font("TimesRoman", Font.PLAIN, 12);
	private JComboBox comboBox;
	private JComboBox comboBox_1;
	private JComboBox comboBox_2;
	private JComboBox comboBox_3;
	String[] frimaly = { "均衡字体", "系统字体", "默认字体" };//字体
	String[] style = {"粗体","斜体","普通"};//类型
	String[] color = {"red","black","blue"};//颜色
	String[] size= {"7","9","12"}; //大小
	private String family = "TimesRoman";
	private String  styleee = "粗体";
	private Color col = Color.black;
	private int   fontSize = 13;
	private JScrollPane scrollPane_1;
	public IMChatView(User friend) {
		this.friend = friend;
		this.setContentPane(new MyPanel());
		getContentPane().setLayout(null);
		JLabel photo = new JLabel();
		ImageIcon im = new ImageIcon("images/" + friend.getPhoto());
		photo.setIcon(im);
		photo.setBounds(10, 10, 63, 65);
		getContentPane().add(photo);
		
		JLabel sign = new JLabel("签名");
		//sign.set
		sign.setSize(70, 100);
		sign.setText("个性签名：" + friend.getSign());
		sign.setBounds(83, 10, 278, 65);
		getContentPane().add(sign);
		
		JLabel friendshow = new JLabel("");
		ImageIcon fndshow = new ImageIcon("images/" + "show6.gif");
		friendshow.setIcon(fndshow);
		friendshow.setBounds(442, 75, 93, 169);
		//getContentPane().add(friendshow);
		
		JLabel myshow = new JLabel("");
		ImageIcon mshow = new ImageIcon("images/" + "show2.gif");
		myshow.setIcon(mshow);
		myshow.setBounds(442, 250, 93, 169);
		//getContentPane().add(myshow);
		
		
		messageConPane = new JTextPane();
		messageConPane.setEditable(false);
		//messageConPane.setLineWrap(true);
		messageConPane.setBounds(10, 85, 326, 222);
		scrollPane = new JScrollPane(messageConPane);
		scrollPane.setBounds(10, 72, 416, 305);
		getContentPane().add(scrollPane);
		
		send_message = new JTextPane();
		send_message.addKeyListener(this);
		//send_message.setLineWrap(true);
		//send_message.setBounds(10, 350, 326, 107);
		scrollPane_1 = new JScrollPane(send_message);
		scrollPane_1.setBounds(10, 420, 416, 85);
		getContentPane().add(scrollPane_1);
		
		JButton send_button = new JButton("发送");
		send_button.addActionListener(new TFListener());
		send_button.setBounds(330, 510, 93, 23);
		getContentPane().add(send_button);
		
		
		comboBox = new JComboBox(frimaly);
		//comboBox.insertItemAt("请选择", 0);
		comboBox.setSelectedIndex(0);
		comboBox.addItemListener(new fontListener());
		comboBox.setBounds(10, 386, 65, 25);
		getContentPane().add(comboBox);
		
		
		comboBox_1 = new JComboBox(style);
		comboBox_1.addItemListener(new fontListener());
		comboBox_1.setBounds(80, 386, 65, 25);
		getContentPane().add(comboBox_1);
		
		comboBox_2 = new JComboBox(size);
		comboBox_2.addItemListener(new fontListener());
		comboBox_2.setBounds(150, 386, 65, 25);
		getContentPane().add(comboBox_2);
		
		comboBox_3= new JComboBox(color);
		comboBox_3.setBounds(220, 386, 65, 25);
		comboBox_3.addItemListener(new fontListener());
		getContentPane().add(comboBox_3);
		
		
		
		this.setBounds(200, 200, 550, 580);
		this.setTitle("与" + friend.getNickname() + "聊天中");
		this.setResizable(false);
		this.setIconImage(Toolkit.getDefaultToolkit().createImage("images/im.png"));
		this.setVisible(true);
		this.addWindowListener(new winAdapt());
		//this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	 private class MyPanel extends JPanel {
		  public void paintComponent(Graphics g) {
		      Graphics2D g2 = (Graphics2D)g;
		      super.paintComponent(g);
		      Image img = Toolkit.getDefaultToolkit().getImage("images/client/chat_background.jpg");
		      g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
	     }
	 }
	 public void showMessage(Message recmessage)
	{
		 flushPane("\n\n"+ friend.getNickname() +": " +recmessage.getSendTime() +'\n'+recmessage.getCon());
	}
	private class TFListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			
			if(!send_message.getText().equals(""))
			{
				Message message = new  Message();
				message.setSenderId(ClientTool.currentUser.getUserid());
				message.setGetterId(friend.getUserid());
				message.setMesType(MessageType.message_comm_textmsg);
				message.setCon(send_message.getText());
				message.setSendTime(new DateProcess().lProcess());
				ClientTool.sendMsg(message);
				//messageConPane.setText( ClientTool.currentUser.getNickname() + new DateProcess().lProcess() +'\n'+send_message.getText()+"\n");
				flushPane("\n\n"+ClientTool.currentUser.getNickname() + new DateProcess().lProcess() +'\n'+send_message.getText());
			}
		}
		
	}
	 class winAdapter extends WindowAdapter{
		 JFrame jf;
		 winAdapter(Object o){
			 this.jf = (JFrame)o;
		 }
		public void windowClosing(WindowEvent windowEvent){
			ClientTool.clientviews.remove(friend.getUserid());
			jf.dispose();
		 }
	 }
	
	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
	 */
	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if(e.getKeyCode() == KeyEvent.VK_ENTER)
		{
			if(!send_message.getText().equals(""))
			{
				Message message = new  Message();
				message.setSenderId(ClientTool.currentUser.getUserid());
				message.setGetterId(friend.getUserid());
				message.setMesType(MessageType.message_comm_textmsg);
				message.setCon(send_message.getText());
				message.setSendTime(new DateProcess().lProcess());
				ClientTool.sendMsg(message);
				//messageConPane.setText( ClientTool.currentUser.getNickname() + new DateProcess().lProcess() +'\n'+send_message.getText()+"\n");
				flushPane("\n\n"+ClientTool.currentUser.getNickname() + new DateProcess().lProcess() +'\n'+send_message.getText());
			}
		}else
		{
			setDocs();
		}	
	}
	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
	 */
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		if(e.getKeyCode() == KeyEvent.VK_ENTER)
		{
			send_message.setText("");
		}
	}
	/* (non-Javadoc)
	 * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
	 */
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stubu

	}
	 public  void setDocs()   {  
		 String str = send_message.getText();
		 //send_message = (JTextArea)send_message;
		 send_message.setText("");
         SimpleAttributeSet   attrSet   =   new   SimpleAttributeSet(); 
         //字体
         StyleConstants.setFontFamily(attrSet, family);
         //类型
         if(styleee.equals("粗体"))
         {
        	 StyleConstants.setBold(attrSet, true);
         }else if(styleee.equals("斜体"))
         {
        	 StyleConstants.setItalic(attrSet, true);
        	 
         }
         //颜色   
         StyleConstants.setForeground(attrSet,   col);   
         //字体大小   
         StyleConstants.setFontSize(attrSet,   fontSize);
         //insert(str,   attrSet);  
         Document doc = send_message.getDocument();
         try {
			doc.insertString(doc.getLength(), str, attrSet);
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(send_message, "对不起，因为你的系统不支持该字体");
			e.printStackTrace();
		}
     }
	 /*  
	 public void flushPane()
	 {
		 //String str = messageConPane.getText();
		 send_message.setText("");
		 //send_message = (JTextArea)send_message;
		 //messageConPane.setText("");
         
		 SimpleAttributeSet   attrSet   =   new   SimpleAttributeSet(); 
         //字体
         StyleConstants.setFontFamily(attrSet, family);
         //类型
         if(styleee.equals("粗体"))
         {
        	 StyleConstants.setBold(attrSet, true);
         }else if(styleee.equals("斜体"))
         {
        	 StyleConstants.setItalic(attrSet, true);
        	 
         }
         //颜色   
         StyleConstants.setForeground(attrSet,   col);   
         //字体大小   
         StyleConstants.setFontSize(attrSet,   fontSize);
         //insert(str,   attrSet);  
         Document doc = messageConPane.getDocument();
         try {
			doc.insertString(doc.getLength(), str, attrSet);
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(messageConPane, "对不起，因为你的系统不支持该字体");
			e.printStackTrace();
		}
		
	 }
	 */
	 public void flushPane(String str)
	 {
		 //String str = send_message.getText();
		 //send_message = (JTextArea)send_message;
		 //messageConPane.setText("");
         SimpleAttributeSet   attrSet   =   new   SimpleAttributeSet(); 
         //字体
         StyleConstants.setFontFamily(attrSet, family);
         //类型
         if(styleee.equals("粗体"))
         {
        	 StyleConstants.setBold(attrSet, true);
         }else if(styleee.equals("斜体"))
         {
        	 StyleConstants.setItalic(attrSet, true);
        	 
         }
         //颜色   
         StyleConstants.setForeground(attrSet,   col);   
         //字体大小   
         StyleConstants.setFontSize(attrSet,   fontSize);
         //insert(str,   attrSet);  
         Document doc = messageConPane.getDocument();
         try {
			doc.insertString(doc.getLength(), str, attrSet);
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(messageConPane, "对不起，因为你的系统不支持该字体");
			e.printStackTrace();
		}
		messageConPane.selectAll();
		send_message.setText("");
	 }
	class fontListener implements ItemListener
	{
		/* (non-Javadoc)
		 * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
		 */
		@Override
		public void itemStateChanged(ItemEvent e) {
			// TODO Auto-generated method stub
			if(e.getSource() == comboBox)
			{
				if(e.getItem().equals(frimaly[0]))
				{
					family = "TimesRoman";
				}else if(e.getItem().equals(frimaly[1]))
				{
					family = "Courier";
				}else if(e.getItem().equals(frimaly[2]))
				{
					family = "Arial";				
				}
				
			}else if(e.getSource() == comboBox_1)
			{
				if(e.getItem().equals(style[0]))
				{
					styleee = style[0];
				}else if(e.getItem().equals(style[1]))
				{
					styleee = style[1];
				}
			}else if(e.getSource() == comboBox_2)			
			{
				if(e.getItem().equals("7"))
				{
					fontSize = 16;
				}else if(e.getItem().equals("9"))
				{
					fontSize = 20;
				}else if(e.getItem().equals("12"))
				{
					fontSize = 25;
				}
				
			}else if(e.getSource() == comboBox_3)
			{
				if(e.getItem().equals("red"))
				{
					col = Color.red;
				}else if(e.getItem().equals("black"))
				{
					col = Color.black;
				}else if(e.getItem().equals("blue"))
				{
					col = Color.blue;
				}
			}
		}
	}
	class winAdapt extends WindowAdapter{
		public void windowClosing(WindowEvent windowEvent){
			ClientTool.clientviews.remove(friend.getUserid());
			dispose();
		}
	}
	public static void main(String[] args)
	{
		User friend = new User("maspacMan","男","做事认真，能挣钱；做人太认真，只会受伤；做人应该像演戏，虚假但轻松。","4.gif",23);
		IMChatView ui = new IMChatView(friend);
		ui.setVisible(true);
	}
}

	
	
	


